
/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.datagather.twitter.acount;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.tompai.datagather.model.TwitterAcounts;
import com.tompai.datagather.twitter.ShadowSockets5Proxy;

/**
* @desc: datagather-tools
* @name: TwitterFollowInitial.java
* @author: tompai
* @createTime: 2018年5月8日 下午10:29:50
* @history:
* @version: v1.0
*/

public class TwitterFollowInfo {

	public static final Logger LOG = LoggerFactory.getLogger(TwitterFollowInfo.class);
	
	private static String homepage="https://twitter.com/";
	private String acount;
	public static boolean isSecurity=false; 
	public static boolean isErrorPage=false; 
	public static String position;
	
	
	public boolean isSecurity() {
	
		return isSecurity;
	}

	public String getAcount() {
	
		return acount;
	}
	
	public static String getPosition() {
	
		return position;
	}
	
	public void setAcount(String acount) {
	
		this.acount = acount;
	}

	public static List<TwitterAcounts> initialFollow(String acount,String role){
		
		List<TwitterAcounts> acountlist=new ArrayList<>();
		String url=homepage+acount+"/"+role;
		LOG.info("URL: "+url);
		String html=null;
		String original_position;
		String loginStats;
		html = ShadowSockets5Proxy.twitterLink(url);
		if(StringUtils.isEmpty(html)) {
			LOG.error("Failed follow url : "+url);
			return null;
		}
		JSONObject json = JSONObject.parseObject(html);
		Document doc = Jsoup.parse(json.getString("page"));
		//LOG.info("Follow page html: "+doc.html());
		//==================================
		loginStats=doc.select(".signin-wrapper").text();
		String submit="登录";
		if(StringUtils.contains(loginStats, submit)) {
			LOG.error("The Cookie expired: "+url);
			return null;
		}
		//==================================
		//账号被冻结//
		String suspendError="Account suspended";
		String suspendedPage=doc.select(".flex-module.error-page.clearfix").text();
		if(StringUtils.contains(suspendedPage, suspendError)) {
			isErrorPage=true;
			LOG.error("Account suspended: "+url);
			return null;
		}
		//==================================
		//访问的页面不存在//
		String searchSeqerA="Sorry, that page doesn’t exist!";
		//String searchSeqerB="对不起，该页面不存在！";
		String errorPage=doc.select(".errorpage-body-content").text();
		//LOG.error("errorPage: "+errorPage);
		if (StringUtils.contains(errorPage, searchSeqerA)) {
			isErrorPage=true;
			LOG.error("Error Page URL: "+url);
			return null;
		}
		original_position=doc.select("div.GridTimeline-items.has-items")
				.attr("data-min-position");
		position=original_position;
		String tweetSecurity=doc.select("h2.ProtectedTimeline-heading").text();
		//==================================
		//账号设置了隐私保护//
		String searchSeq="This account's Tweets are protected.";
		if(StringUtils.contains(tweetSecurity, searchSeq)) {
			LOG.error(acount +"-Only confirmed followers have access to 's Tweets and complete profile. Click the \"Follow\" button to send a follow request.");
			isSecurity=true;
			return null;
		}
		Elements elements=doc.select(".Grid-cell.u-size1of2.u-lg-size1of3.u-mb10");
		for (Element element : elements) {
			String screenName=element.select("b.u-linkComplex-target").text();
			String userName=element.select(".fullname.ProfileNameTruncated-link.u-textInheritColor.js-nav").text();
			String profileBio=element.select(".ProfileCard-bio.u-dir").text();
			TwitterAcounts acounts=new TwitterAcounts(userName, screenName, profileBio);
			acountlist.add(acounts);
			//System.out.println(index+": userName: "+userName);
			//System.out.println(index+": screenName: "+screenName);
			//System.out.println(index+": profileBio: "+profileBio);
		}
		return acountlist;
	}
}

